package com.itheima.resources.aliyunmessage;

import com.aliyuncs.CommonRequest;
import com.aliyuncs.CommonResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.profile.DefaultProfile;

/**
 * 短信发送工具类
 */
public class SMSUtils {

    public static String accessKeyId = "LTAI4GKU2AiRvezEfC3FprAq";
    public static String accessKeySecret = "fVRnGrH3nBVJsqrBruiDvm5rJEHVls";

    public static final String VALIDATE_CODE = "SMS_201456112";//发送短信验证码
    public static final String ORDER_NOTICE = "SMS_192571486";//体检预约成功通知


    /**
     * 注册短信发送
     */

    public static String validaCodeSms(String telephone, String randomCode) {
        try {
            DefaultProfile profile = DefaultProfile.getProfile("cn-hangzhou", accessKeyId, accessKeySecret);
            IAcsClient client = new DefaultAcsClient(profile);

            CommonRequest request = new CommonRequest();
            request.setSysMethod(MethodType.POST);
            request.setSysDomain("dysmsapi.aliyuncs.com");
            request.setSysVersion("2017-05-25");
            request.setSysAction("SendSms");
            request.putQueryParameter("RegionId", "cn-hangzhou");
            request.putQueryParameter("PhoneNumbers", telephone);
            request.putQueryParameter("SignName", "传智健康htt");
            request.putQueryParameter("TemplateCode", VALIDATE_CODE);
            //   模板里面 ${code}
            request.putQueryParameter("TemplateParam", "{\"code\":\"" + randomCode + "\"}");

            CommonResponse response = client.getCommonResponse(request);
            System.out.println(response.getData());
            String data = response.getData();
            return data;
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException("短信发送失败：" + e.getMessage());
        }

    }


    /**
     * 预约套餐成功 发送短信通知
     */
    public static String sendMessageTuYou(String telephone, String message) {
        try {
            DefaultProfile profile = DefaultProfile.getProfile("cn-hangzhou", accessKeyId, accessKeySecret);
            IAcsClient client = new DefaultAcsClient(profile);

            CommonRequest request = new CommonRequest();
            request.setSysMethod(MethodType.POST);
            request.setSysDomain("dysmsapi.aliyuncs.com");
            request.setSysVersion("2017-05-25");
            request.setSysAction("SendSms");
            request.putQueryParameter("RegionId", "cn-hangzhou");
            request.putQueryParameter("PhoneNumbers", telephone);
            request.putQueryParameter("SignName", "唐彭");
            request.putQueryParameter("TemplateCode", ORDER_NOTICE);
            //   模板里面 ${code}
            request.putQueryParameter("TemplateParam", "{\"submittime\":\"" + message + "\"}");

            CommonResponse response = client.getCommonResponse(request);
            System.out.println(response.getData());
            String data = response.getData();
            return data;
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException("预约短信发送失败：" + e.getMessage());
        }

    }

}
